package com.yz.user.controller;

import com.yz.model.dto.user.*;
import com.yz.api.facade.user.UserFacade;
import com.yz.user.mapping.UserMapping;
import com.yz.model.entity.user.User;
import com.yz.model.vo.LoginUserVo;
import com.yz.common.resp.BaseResponse;
import com.yz.user.service.UserService;
import com.yz.common.utils.ResultUtils;
import com.yz.common.utils.VerifyUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2023/11/27
 * Time: 19:34
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class UserController implements UserFacade {

    private final UserService userService;

    private final Environment env;

    private final UserMapping userMapping;

    @Override
    public BaseResponse<String> login(UserLoginRequest userLoginRequest) {
        if(userLoginRequest == null){
            throw new RuntimeException("参数不能为空");
        }
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        if(StringUtils.isAnyBlank(username, password)){
            throw new RuntimeException("用户名或密码不能为空");
        }
        String token = userService.userLogin(username, password);
        return ResultUtils.success("登录成功" ,token);
    }

    @Override
    public BaseResponse<String> userLoginWithEmail(UserLoginWithEmailRequest userLoginWithEmailRequest) {
        if(userLoginWithEmailRequest == null){
            throw new RuntimeException("参数不能为空");
        }
        String email = userLoginWithEmailRequest.getEmail();
        String code = userLoginWithEmailRequest.getCode();
        if(StringUtils.isAnyBlank(email, code)){
            throw new RuntimeException("邮箱或验证码不能为空");
        }
        String token = userService.userLoginWithEmail(email, code);
        return ResultUtils.success("登录成功" ,token);
    }

    @Override
    public BaseResponse<Long> register(UserRegisterRequest userRegisterRequest) {
        if(userRegisterRequest == null){
            throw new RuntimeException("参数不能为空");
        }
        String username = userRegisterRequest.getUsername();
        String password = userRegisterRequest.getPassword();
        String email = userRegisterRequest.getEmail();
        String code = userRegisterRequest.getCode();
        Long res = userService.userRegister(username, password, email, code);
        if(res == null){
            throw new RuntimeException("注册失败");
        }
        return ResultUtils.success("注册成功", res);
    }

    public BaseResponse<Long> deleteUser(Long id) {
        return ResultUtils.success(id);
    }

    @Override
    public BaseResponse<Boolean> updatePassword(Long uid, UpdatePasswordRequest updatePasswordRequest) {
        if(updatePasswordRequest == null){
            throw new RuntimeException("参数不能为空");
        }
        userService.updatePassword(uid, updatePasswordRequest);
        return ResultUtils.success("更新成功");
    }

    @Override
    public BaseResponse<Boolean> getCode(String email) {
        if(StringUtils.isBlank(email)){
            throw new RuntimeException("邮箱不能为空");
        }
        if (!VerifyUtils.isEmail(email)){
            throw new RuntimeException("邮箱格式不正确");
        }
        userService.getCode(email);
        return ResultUtils.success("发送成功");
    }

    @Override
    public BaseResponse<Boolean> checkUsername(String info) {
        if(StringUtils.isBlank(info)){
            throw new RuntimeException("参数不能为空");
        }
        if(VerifyUtils.isEmail(info)){
            if(userService.findUserByEmail(info) != null){
                throw new RuntimeException("邮箱已存在");
            }
            return ResultUtils.success("邮箱可用");
        }
        if(userService.findUserByUserName(info) != null){
            throw new RuntimeException("用户名已存在");
        }
        return ResultUtils.success("用户名可用");
    }

    @Override
    public BaseResponse<LoginUserVo> getCurrentUserInfo(String username) {
        User user = userService.findUserByUserName(username);
        if (user == null){
            throw new RuntimeException("获取用户信息失败");
        }
        LoginUserVo loginUserVo = userMapping.convertTo(user);
        return ResultUtils.success(loginUserVo);
    }

    @Override
    public LoginUserVo getUserById(Long id) {
        if(StringUtils.isEmpty(id.toString())){
            throw new RuntimeException("参数不能为空");
        }
        User user = userService.getUserById(id);
        LoginUserVo userVO = userMapping.convertTo(user);
        return userVO;
    }

    @Override
    public void updateUserCountPromotion(Long id, Integer countPromotion) {
        if(StringUtils.isAnyBlank(id.toString(), countPromotion.toString())){
            throw new RuntimeException("参数不能为空");
        }
        if(!userService.updateUserCountPromotion(id, countPromotion)){
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public BaseResponse consumeAmount(Long uid, Long amount) {
        if(StringUtils.isAnyBlank(uid.toString(), amount.toString())){
            throw new RuntimeException("参数不能为空");
        }
        userService.consumeAmount(uid, amount);
        return ResultUtils.success("消费成功");

    }

    @Override
    public BaseResponse<Boolean> reChargeAmount(Long uid, Long amount) {
        if(StringUtils.isAnyBlank(uid.toString(), amount.toString())){
            throw new RuntimeException("参数不能为空");
        }
        userService.reChargeAmount(uid, amount);
        return ResultUtils.success("充值成功");
    }

    @Override
    public BaseResponse<Boolean> logout(String username, HttpServletResponse response) {
        if(StringUtils.isBlank(username)){
            throw new RuntimeException("参数不能为空");
        }
        userService.logout(username);
        response.setHeader("Authorization", "");
        return null;
    }

    @Override
    public BaseResponse<Long> updateUser(Long uid, UpdateUserInfoRequest updateUserInfoRequest) {
        if(updateUserInfoRequest == null){
            throw new RuntimeException("参数不能为空");
        }
        User user = new User();
        user.setId(uid);
        BeanUtils.copyProperties(updateUserInfoRequest, user);
        boolean res = userService.updateById(user);
        if(!res){
            throw new RuntimeException("更新失败");
        }
        return ResultUtils.success(uid);
    }

    @Override
    public BaseResponse<Boolean> updateCountPromotion(Long uid, Integer countPromotion) {
        if(StringUtils.isBlank(countPromotion.toString())){
            throw new RuntimeException("参数不能为空");
        }
        userService.updateCountPromotion(uid, countPromotion);
        return null;
    }

    @Override
    public BaseResponse<Boolean> checkEmailIsCurrent(Long uid, String email) {
        if(StringUtils.isBlank(email)){
            throw new RuntimeException("参数不能为空");
        }
        boolean res = userService.checkEmailIsCurrent(uid, email);
        if(!res){
            throw new RuntimeException("请输入当前账户的邮箱");
        }
        return ResultUtils.success(true);
    }

}
